import {defineStore} from 'pinia'
import {Product} from '../api'
import {getProducts} from "../api/index"

export const useProductStore = defineStore('product',{
    state:() => {
        return {
            allProducts:[] as Product[] // as是类型断言
        }
    },

    getters:{},

    actions:{
        // 获取所有商品
        async getAllProducts(){
            const res = await getProducts()
            this.allProducts = res
        },

        // 更新库存
        decrementProduct(product:Product){
            const p = this.allProducts.find(item => item.id === product.id)
            if(p){
                p.inventory--
            }
        }
    }
})
